package com.bhs.sansonglogistics.ui.enterprise;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.os.Handler;
import android.util.ArrayMap;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.CollaborationBean;
import com.bhs.sansonglogistics.bean.CollaborationDataBean;
import com.bhs.sansonglogistics.dialog.BottomSelectDialog;
import com.bhs.sansonglogistics.dialog.BottomSelectPhone2Dialog;
import com.bhs.sansonglogistics.dialog.BottomSelectiveLineDialog;
import com.bhs.sansonglogistics.dialog.MyClickListener;
import com.bhs.sansonglogistics.dialog.MyClickListener2;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.AddEnterpriseAdapter;
import com.bhs.sansonglogistics.ui.adapter.CollaborationAdapter;
import com.bhs.sansonglogistics.ui.order.ShipmentsActivity;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 添加企业
 */
public class AddEnterpriseActivity extends BaseActivity implements NetCallBack {

    private EditText mEtSearch;
    private RecyclerView mRvList;
    private AddEnterpriseAdapter mAdapter;
    private SwipeRefreshLayout mRefreshLayout;
    private int page=1;

    @Override
    public int initLayout() {
        return R.layout.activity_add_enterprise;
    }

    @Override
    public void initView() {

        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("添加企业");
        mEtSearch = findViewById(R.id.et_search);
        mRvList = findViewById(R.id.rv_list);
        mEtSearch.setOnKeyListener(new View.OnKeyListener() {//输入完后按键盘上的搜索键【回车键改为了搜索键】
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {//改动回车键功能
                    map.put("search_key", mEtSearch.getText().toString());
                    loadData();
                }
                return false;
            }
        });
        mRefreshLayout = findViewById(R.id.refreshLayout);

        //监听 刷新是回调
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();

            }
        });
    }

    Map<String, String> map = new ArrayMap<>();

    private void loadData() {
        map.put("page", String.valueOf(page));
        networkRequest(netApi.getLogisticsCompanyList(map), this);
    }

    @Override
    public void initData() {
        mAdapter = new AddEnterpriseAdapter();
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(this));

        mAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.layout_empty, null));
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CollaborationBean data = mAdapter.getData().get(position);
                if (view.getId() == R.id.ll_contact) {
                    call(data);
                } else if (view.getId() == R.id.tv_consent) {
                    add(data.getLogistics_id());
                }
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        //loadData();
    }

    private BottomSelectPhone2Dialog bottomSelectDialog;
    private BasePopupView basePopupView;

    private void call(CollaborationBean data) {
        if (basePopupView == null) {
            bottomSelectDialog = new BottomSelectPhone2Dialog(this, data.getMobile(), data.getFixed_telephone(), data.getLine_info().getLine_mobile());
            basePopupView = new XPopup.Builder(this)
                    .asCustom(bottomSelectDialog)
                    .show();
            bottomSelectDialog.setMyClickListener(new MyClickListener() {
                @Override
                public void onClick(View v) {
                    networkRequest(netApi.company_calll_company(data.getLogistics_id()),null);
                }
            });
        } else {
            basePopupView.show();
        }
    }

    BottomSelectiveLineDialog bottomSelectiveLineDialog;

    private void add(int logistics_id) {
        if (bottomSelectiveLineDialog == null) {
            bottomSelectiveLineDialog = new BottomSelectiveLineDialog(mActivity, 2);
        }
        bottomSelectiveLineDialog.setMyClickListener(new MyClickListener2() {
            @Override
            public void onClick(String id, String info) {
                Map<String, String> map = new ArrayMap<>();
                map.put("to_logistics_id", String.valueOf(logistics_id));
                map.put("line_id", id);
                map.put("is_show_address", info);
                networkRequest(netApi.applyCollaborate(map), AddEnterpriseActivity.this, 9527);
            }
        });
        bottomSelectiveLineDialog.show();
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 9527) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            Toast.makeText(mContext, bean.getMsg(), Toast.LENGTH_SHORT).show();
            if (bean.isStatus()) {
                finish();
            }
        } else {
            CollaborationDataBean bean = new Gson().fromJson(json, CollaborationDataBean.class);
            mRefreshLayout.setRefreshing(false);
            if (bean.isStatus()) {
                if (page == 1) {
                    mAdapter.setNewData(bean.getData().getList());
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(bean.getData().getList());
                }
                if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                    mAdapter.loadMoreEnd();
                }
            } else {
                ToastUtil.show(bean.getMsg());
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        if (requestCode == 0) {
            mRefreshLayout.setRefreshing(false);
            mAdapter.loadMoreComplete();
        }
    }
}